/**
 * Ref https://blog.csdn.net/chase_hung/article/details/82284788
 */

// Qt
#include <QLabel>
#include <QPalette>
#include <QPushButton>
#include <QQmlComponent> // include qqml.h
#include <QQmlContext>
#include <QQuickItem>
#include <QQuickWidget>

// my
#include "JsonDataCom.h"
#include "MyWindow.h"

MyWindow::MyWindow(QWidget* parent)
    : QWidget(parent)
    , m_headerQuick(nullptr)
    , m_bodyQuick(nullptr)
    , m_pBtnResize(nullptr)
    , m_model()
    , m_zoomIn(false)
    , m_top(60) {

    // this->setWindowFlags(Qt::FramelessWindowHint);
    // this->setAttribute(Qt::WA_TranslucentBackground);
    this->resize(800, 500);

    // register JsonDataCom type
    qmlRegisterType<JsonDataCom>("cn.JsonDataCom", 1, 0, "JsonDataCom");

    // add widget
    build();
}
//---------------------------------
MyWindow::~MyWindow() {
}
//---------------------------------
int MyWindow::actionDialog(QString type) {
    qDebug() << "MyWindow::actionDialog(" << type << ") ";
    int ec = bodyClose();

    // return 0;
    if (type == "close") return ec;

    ec = bodyOpen();
    if (ec > 0) return ec;
    QString url = "";
    if (type == "rect") {
        url = "qrc:/MyWidget.qml";
    }
    else if (type == "model") {
        url = "qrc:/MyWidgetListView.qml";
    }
    else if (type == "json") {
        url = "qrc:/JsonDataView.qml";
    }
    else {
        qDebug() << " type is undifined ";
    }

    qDebug() << "     url = " << url;
    m_bodyQuick->setSource(QUrl(url));

    // treat only for rect
    if (type == "rect") {
        buildRect();
    }
    return 0;
}
//---------------------------------
void MyWindow::build() {

    // load qml
    int x = 5;
    int y = 10;
#if 0
    // 设置背景，方法1：使用Style Sheet，一般不用QSS设置窗口背景，也不建议使用。
    // （这里是对于窗口而，如果是子部件当然可以）。因为窗口使用QSS设置背景之后，
    // 若子部件不使用同样的方式来设置，默认则会继承父窗口的样式。

     this->setStyleSheet("background-color: lightgray; color: naviy;");
#else
    // 设置背景，方法2：使用QPalette
    QPalette pal(this->palette());
    pal.setColor(QPalette::Background, Qt::lightGray); //设置背景色
    this->setAutoFillBackground(true);
    this->setPalette(pal);
#endif
    m_pBtnResize = new QPushButton(this);
    m_pBtnResize->resize(60, 40);
    m_pBtnResize->move(x, y);
    m_pBtnResize->setText("resize");

    // title
    x = x + m_pBtnResize->width() + 5;
    y = 0;

    m_headerQuick = new QQuickWidget(this);
    m_headerQuick->rootContext()->setContextProperty("cp_MyDialog", this);

    // set sample model
    m_model.sample(); // set sample data
    m_headerQuick->rootContext()->setContextProperty("$Model", &m_model);
    m_headerQuick->setSource(QUrl("qrc:/MyTitle.qml"));
    m_headerQuick->setResizeMode(QQuickWidget::SizeRootObjectToView);
    m_headerQuick->setGeometry(x, y, width() - x, m_top);
    m_headerQuick->show();

    // after load qml get QuickItem
    QQuickItem* item = m_headerQuick->rootObject();
    if (item) {
        // set property of qml by C++ directly
        item->setProperty("modelSource", 1);

        // connect qml signal to c++ slot
        connect(item, SIGNAL(sendType(QString)), this, SLOT(actionDialog(QString)),
                Qt::QueuedConnection);
    }
}
//---------------------------------
int MyWindow::buildRect() {
    if (!m_bodyQuick) return 1;

    QQuickItem* item = m_bodyQuick->rootObject();
    if (item) {
        item->setProperty("displayWidth", 400);
        item->setProperty("displayHeight", 300);
    }

    // use C++11 , Lambda formula, [=]
    connect(m_pBtnResize, &QPushButton::clicked, [ = ] {
        if (nullptr == m_bodyQuick) return;
        QQuickItem* item = m_bodyQuick->rootObject();
        if (nullptr == item) return;

        double ratio = m_zoomIn ? 0.8 : 0.4;
        m_zoomIn     = !m_zoomIn;
        item->setProperty("displayWidth", 800 * ratio);
        item->setProperty("displayHeight", 600 * ratio);
    });

    return 0;
}
//---------------------------------
int MyWindow::bodyClose() {
    if (m_bodyQuick) {
        delete m_bodyQuick;
        m_bodyQuick = nullptr;
        disconnect(m_pBtnResize, 0, 0, 0);
    }
    return 0;
}
//---------------------------------
int MyWindow::bodyOpen() {
    if (m_bodyQuick) return 0;

    m_bodyQuick = new QQuickWidget(this);

    m_bodyQuick->setResizeMode(QQuickWidget::SizeRootObjectToView);

    m_bodyQuick->setGeometry(0, m_top, width(), height() - m_top);
    m_bodyQuick->show();

    // 设置背景，方法2：使用QPalette
    QPalette pal(m_bodyQuick->palette());
    pal.setColor(QPalette::Background, Qt::darkGray); //设置背景色
    m_bodyQuick->setAutoFillBackground(true);
    m_bodyQuick->setPalette(pal);

    return 0;
}
//---------------------------------
void MyWindow::resizeEvent(QResizeEvent* event) {
    QWidget::resizeEvent(event);
    if (m_headerQuick) {
        m_headerQuick->resize(width() - m_headerQuick->x(), m_top);
    }

    if (m_bodyQuick) {
        m_bodyQuick->resize(width(), height() - m_top);
    }
}
